#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->keyboard = new LaserKeyboard(this, ui->textEdit);
    connect(ui->pushButton, SIGNAL(pressed()), this, SLOT(buttonPressedSlot()) );
    connect(ui->pushButton, SIGNAL(released()), this, SLOT(buttonReleasedSlot()));
    connect(this->keyboard, SIGNAL(countChanged(int)), this, SLOT(changeLabelSlot(int)));
    this->keyboard->start();
}

void MainWindow::buttonPressedSlot()
{

    //ui->textEdit->textCursor().insertText(QString("S"));
    //ui->textEdit->setPlainText(QString("ssssss"));
    //this->repaint();

    QKeyEvent *event1 =  new QKeyEvent(QEvent::KeyPress, 's', Qt::ShiftModifier, QString('S'));
    QApplication::postEvent(ui->textEdit, event1);
    //QApplication::sendEvent(ui->textEdit, &event1);

    //QApplication::sendEvent(ui->textEdit, &event2);

}

void MainWindow::buttonReleasedSlot()
{
    QKeyEvent *event2 = new QKeyEvent(QEvent::KeyRelease, 's', Qt::NoModifier, QString('s'));
    QApplication::postEvent(ui->textEdit, event2);
}

void MainWindow::changeLabelSlot(int count)
{
    static int total = 0;
    total++;
    ui->label->setNum(total);
}

MainWindow::~MainWindow()
{
    this->keyboard->terminate();
    this->keyboard->wait();
    delete this->keyboard;
    delete ui;
}
